<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="spec/resources/custom-elements-helpers.js"></script>
<body>
<script>
'use strict';
(() => {

if (!window.testRunner) {
  // Requires evaluateScriptInIsolatedWorld
  return;
}

promise_test((t) => {
  return create_window_in_test(t)
  .then((w) => {
    function in_isolated_world() {
      let f = document.querySelector('iframe');
      let w = f.contentWindow;
      let e = w.document.body.lastChild;
      w.postMessage(
          e.localName == 'a-a' &&
          Object.getPrototypeOf(e) === w.HTMLElement.prototype,
          '*');
    }

    var p = new Promise((resolve) => {
      w.addEventListener('message', t.step_func((event) => {
        assert_true(event.data,
                    'the custom element prototype should not appear in ' +
                    'isolated worlds');
        resolve();
      }));
    });

    class X extends w.HTMLElement {}
    w.customElements.define('a-a', X);
    w.document.body.appendChild(new X);

    testRunner.evaluateScriptInIsolatedWorld(
      1,
      `(${in_isolated_world.toString()})();`);

    return p;
  });
}, 'Retrieve a custom element in an isolated world');

// TODO(dominicc): Test retrieving a custom element that has not been wrapped
// before in any world, when upgrades are implemented.

promise_test((t) => {
  return create_window_in_test(t)
  .then((w) => {
    function in_isolated_world() {
      let f = document.querySelector('iframe');
      let w = f.contentWindow;
      w.postMessage(`customElements=${w.customElements}`, '*');
    }

    var p = new Promise((resolve) => {
      w.addEventListener('message', t.step_func((event) => {
        assert_equals(event.data,
                      'customElements=null',
                      'the customElements property should be null ' +
                      'in isolated worlds');
        resolve();
      }));
    });

    testRunner.evaluateScriptInIsolatedWorld(
      1,
      `(${in_isolated_world.toString()})();`);

    return p;
  });
}, 'No custom elements registry in isolated worlds');

})();
</script>
